package com.saltedfish.tool.ui.flag.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.widget.LinearLayout
import android.widget.TextView
import cn.droidlover.xdroidmvp.base.SimpleListener
import cn.droidlover.xdroidmvp.log.XLog
import cn.droidlover.xdroidmvp.router.Router
import cn.droidlover.xdroidmvp.view.widget.HorizontalProgressBar
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.saltedfish.tool.R
import com.saltedfish.tool.model.SmallTable
import com.saltedfish.tool.ui.flag.SonFlagDetailActivity
import com.saltedfish.tool.util.DateUtils
import kotlinx.android.synthetic.main.activity_son_flag_detail.*
import java.text.DecimalFormat


class SubFlagAdapter(lists: List<SmallTable>, color:Int,type:Int) :
    BaseQuickAdapter<SmallTable, BaseViewHolder>(
        R.layout.item_sub_flag,
        lists
    ) {

    private var colors :Int = color

    //0从flag界面，1从flag详情，2从成就界面
    private var status :Int = type


    private var listener: SimpleListener<*>? = null


    fun setListener(listener: SimpleListener<*>?) {
        this.listener = listener
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder?, item: SmallTable) {
        var mItemSubFlag = helper?.getView<LinearLayout>(R.id.mItemSubFlag)
        var flagSonContent = helper?.getView<TextView>(R.id.flagSonContent)

        var flagProgress = helper?.getView<TextView>(R.id.flagProgress)
        var mProgressBar = helper?.getView<HorizontalProgressBar>(R.id.mProgressBar)
        var sub_detail = helper?.getView<LinearLayout>(R.id.sub_detail)

        flagSonContent!!.setTextColor(mContext.resources.getColor(R.color.color_333333))
        flagSonContent?.text = item.title
        var position = helper!!.adapterPosition

        sub_detail!!.setOnClickListener {
//            val intent = Intent(mContext,SonFlagDetailActivity.class)
//            intent.putExtra("subId", item.subId)
//            mContext.startActivity(intent)
            Router.newIntents(mContext)
                    .putString("subId", item.subId)
                    .putString("status", status.toString())
                    .to(SonFlagDetailActivity::class.java).launch()
        }


        if(colors == 0){
            mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_FF6868)
            mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_FF6868)
        }else if(colors == 1){
            mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_FF9B4B)
            mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_FF9B4B)
        }else if(colors == 2){
            mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_FFC733)
            mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_FFC733)
        }else if(colors == 3){
            mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_A3E946)
            mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_A3E946)
        }else if(colors == 4){
            mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_3097FF)
            mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_3097FF)
        }else if(colors == 5){
            mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_CA7AFF)
            mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_CA7AFF)
        }

//        mItemSubFlag!!.visibility = View.VISIBLE
   //     XLog.e("-subflag>>>>---${item}---------")

        if(item.status == "提前完成"){
            var completeDay = item.completeDay!!.replace("-","/")
            flagProgress!!.text = completeDay +  "完成"
            flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_333333))
            mProgressBar!!.progress = 100

        }else{
                mProgressBar!!.progress = (item.progress * 100).toInt()
                if(item.progress == 1.0f){
                    var endTime = item.endTime!!.replace("-","/")
                    flagProgress!!.text = endTime +  "完成"
                    flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_333333))

                }else{
                    //进行的状态
                    var startTime = DateUtils.compareEqualsDate(item.startTime, DateUtils.getStringDateShort())
                    if(startTime != 0){
                        var day = DateUtils.compareEqualsDate(item!!.endTime, DateUtils.getStringDateShort())
                        if(day != 1){
                            if (item.status == "进行中") {
//                            val df = DecimalFormat("######0.00")
//                            flagProgress!!.text = df.format((item.progress * 100))  + "%"
                                if(item.progress == null || item.progress == 0f){
                                    flagProgress!!.text =  "0%"
                                }else{
                                    flagProgress!!.text = String.format("%.1f",(item.progress * 100)) + "%"
                                }

                                flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_333333))

                            } else if (item.status == "暂停") {
                                mProgressBar!!.progressColor = mContext.resources.getColor(R.color.color_E1E4E8)
                                mProgressBar!!.borderColor = mContext.resources.getColor(R.color.color_E1E4E8)
                                flagProgress!!.text =  "暂停"
                                flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_999999))

                                flagSonContent!!.setTextColor(mContext.resources.getColor(R.color.color_999999))

                            } else if (item.status == "提前完成") {
                                var completeDay = item.completeDay!!.replace("-","/")
                                flagProgress!!.text = completeDay +  "完成"
                                flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_333333))
                            }
                        }else{
                            flagProgress!!.text =  "已延期"
                            flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_ED5656))

                        }
                    }else{
                        var startTime = item.startTime!!.replace("-","/")
                        flagProgress!!.text = startTime + "开始"
                        flagProgress!!.setTextColor(mContext.resources.getColor(R.color.color_333333))

                }

            }
        }


    }
}